// const { isRegExp } = require("util")

function formatTime(date) {  
  var year = date.getFullYear()  
  var month = date.getMonth() + 1  
  var day = date.getDate()  
  
  var hour = date.getHours()  
  var minute = date.getMinutes()  
  var second = date.getSeconds()  
  
  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')  
}
function cnformatTime(date) {  
  var year = date.getFullYear()  
  var month = date.getMonth() + 1  
  var day = date.getDate()  
  
  var hour = date.getHours()  
  var minute = date.getMinutes()  
  var second = date.getSeconds()  
  
  return year + '年'+formatNumber(month)+'月'+formatNumber(day)+'日 ' + [hour, minute].map(formatNumber).join(':')  
} 
function formatNumber(n) {  
  n = n.toString()  
  return n[1] ? n : '0' + n  
}  
  
// 去前后空格  
function trim(str) {  
  return str.replace(/(^\s*)|(\s*$)/g, "");  
}  
  
// 提示错误信息  
function isError(msg, that) {  
  that.setData({  
    showTopTips: true,  
    errorMsg: msg  
  });
  setTimeout(function (that) {
    clearError(that)
  }.bind(that), 5000, that);
}
  
// 清空错误信息  
function clearError(that) {  
  that.setData({  
    showTopTips: false,  
    errorMsg: ""  
  })  
}  

function getDateDiff(dateTimeStamp){
	var minute = 1000 * 60;
	var hour = minute * 60;
	var day = hour * 24;
	var halfamonth = day * 15;
	var month = day * 30;
	var now = new Date().getTime();
	var diffValue = dateTimeStamp - now;
	if(diffValue < 0){return;}
	var monthC =diffValue/month;
	var weekC =diffValue/(7*day);
	var dayC =diffValue/day;
	var hourC =diffValue/hour;
	var minC =diffValue/minute;
  var result = '';
	if(monthC>=1){
		result="" + parseInt(monthC) + "月后";
	}
	else if(weekC>=1){
		result="" + parseInt(weekC) + "周后";
	}
	else if(dayC>=1){
		result=""+ parseInt(dayC) +"天后";
	}
	else if(hourC>=1){
		result=""+ parseInt(hourC) +"小时后";
	}
	else if(minC>=1){
		result=""+ parseInt(minC) +"分钟后";
	}else
	result="刚刚";
	return result;
}

function getDateBiff(dateTimeStamp){
	var minute = 1000 * 60;
	var hour = minute * 60;
	var day = hour * 24;
	var halfamonth = day * 15;
	var month = day * 30;
	var now = new Date().getTime();
	var diffValue = now - dateTimeStamp;
	if(diffValue < 0){return;}
	var monthC =diffValue/month;
	var weekC =diffValue/(7*day);
	var dayC =diffValue/day;
	var hourC =diffValue/hour;
  var minC = diffValue / minute;
  var result = '';
	if(monthC>=1){
		result="" + parseInt(monthC) + "月前";
	}
	else if(weekC>=1){
		result="" + parseInt(weekC) + "周前";
	}
	else if(dayC>=1){
		result=""+ parseInt(dayC) +"天前";
	}
	else if(hourC>=1){
		result=""+ parseInt(hourC) +"小时前";
	}
	else if(minC>=1){
		result=""+ parseInt(minC) +"分钟前";
	}else
	result="刚刚";
	return result;
}

function escape2Html(str) { 
 var arrEntities={'lt':'<','gt':'>','nbsp':' ','amp':'&','quot':'"'}; 
 return str.replace(/&(lt|gt|nbsp|amp|quot);/ig,function(all,t){return arrEntities[t];}); 
}


/**
 * 重新载入指定页面
 */
function reloadPage(route){
  let pages = getCurrentPages();
  for (var i = 0; i < pages.length; i++) {
    let page = pages[i];
    if (page.route == route) {
      page.onLoad(page.options);
      break;
    }
  }
}

function getPage(route, route1){
  let pages = getCurrentPages();
  for (var i = 0; i < pages.length; i++) {
    let page = pages[i];
    if (page.route == route || page.route == route1) {
      return page;
    }
  }
  return undefined;
}

/**
 * 重新载入所有页面
 */
function reloadAllPage(){
  let pages = getCurrentPages();
  pages.forEach(page => {
    page.onLoad(page.options);
  })
}

function compareArray(a1, a2){
  if (!a1 || !a2) return -1;
  for(var i=0; i < a2.length; i++){
    if (i >=a1.length || a2[i] != a1[i]){
      return i;
    }
  }
  return -2;
}

// function array2obj(arr){
//   var obj = false;
//   for(var i=arr.length -1;i>=0;i--){
//     if (!obj)
//       obj = {title: arr[i]};
//     else{
//       obj = {title: arr[i], childs: [obj]};
//     }
//   }
//   return obj;
// }

function compareVersion(v1, v2) {
  v1 = v1.split('.')
  v2 = v2.split('.')
  const len = Math.max(v1.length, v2.length)

  while (v1.length < len) {
    v1.push('0')
  }
  while (v2.length < len) {
    v2.push('0')
  }

  for (let i = 0; i < len; i++) {
    const num1 = parseInt(v1[i])
    const num2 = parseInt(v2[i])

    if (num1 > num2) {
      return 1
    } else if (num1 < num2) {
      return -1
    }
  }

  return 0
}

function checkForm(form, rules){
  let result = {};
  for(var v in rules){
    if (typeof(form[v])==='string') form[v] = form[v].trim();
    if (rules[v]=='require' && (form[v] === undefined || form[v]==='' || (typeof(form[v]) === 'object' && Object.keys(form[v]).length === 0))){
      result[v] = '不能为空'
    }else{
      if (rules[v].regexp && rules[v].tip){
        let reg = new RegExp(rules[v].regexp, rules[v].options || '');
        if (!reg.test(form[v])){
          result[v] = rules[v].tip;
        }
      }
    }
  }
  return result;
}

module.exports = {  
  formatTime, 
  cnformatTime, 
  trim,
  isError,
  clearError,
  getDateDiff,
  escape2Html,
  getDateBiff,
  reloadPage,
  reloadAllPage,
  getPage,
  compareVersion,
  checkForm
}